package com.gxa.pingan.controller;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.Refund;
import com.gxa.pingan.service.RefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 退款的接口
 * @author lijianmin
 * @date 2021/10/25  15:20:14
 */
@CrossOrigin
@RestController
@RequestMapping("/refund")
@Api(value = "退款的接口",tags = "退款接口的API")
public class RefundController {

    @Autowired
    private RefundService refundService;

    /**
     * 退款列表查询的方法,orderType如果为空,则进行全查询,不为空则进行订单状态查询
     * @param orderType 订单状态
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合以及总数
     */
    @GetMapping("/getList")
    @ApiOperation(value = "退款列表查询的方法,orderType如果为空,则进行全查询,不为空则进行订单状态查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderType",value = "订单状态",paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页记录数",required = true,paramType = "query",dataType = "Integer")
    })
    public Map getList(Integer orderType, Integer page, Integer size) throws BusinessException {
        List<Refund> list = refundService.getList(orderType, page, size);
        Integer count = refundService.getListCount(orderType);
        JsonResult jsonResult = new JsonResult();
        if (count != 0) {
            jsonResult.setData(list);
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.put("count",count);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("当前无数据");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 点击订单号进入详情页方法
     * @param orderNum 订单号
     * @return 返回查询到的对象
     */
    @GetMapping("/selectByOrderNum")
    @ApiOperation(value = "点击订单号进入详情页的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNum",value = "订单号",dataType = "String",paramType = "query"),
    })
    public Map selectByOrderNum(@RequestParam("orderNum") String orderNum){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("当前接口未实现");
        return jsonResult.getDataMap();
    }


    /**
     * 驳回退款的方法(修改订单状态为已拒绝(1))
     * @param orderNum 修改状态订单的订单号
     * @param rejReason 驳回退款的理由
     * @return 返回修改后的数据和是否返回成功
     */
    @PostMapping("/refuseRefund")
    @ApiOperation(value = "驳回退款的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNum",value = "驳回退款订单的订单号",required = true,paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "rejReason",value = "驳回退款的理由",required = true,paramType = "query",dataType = "String")
    })
    public Map refuseRefund(@RequestParam("orderNum") String orderNum,@RequestParam("rejReason")String rejReason) throws BusinessException {
        Boolean integer = refundService.refuseRefund(orderNum, rejReason);
        JsonResult jsonResult = new JsonResult();
        if (integer) {
            jsonResult.setMsg("驳回退款成功");
            jsonResult.setCode("1");
        }else {
            jsonResult.setMsg("驳回退款失败");
            jsonResult.setCode("0");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 退款的方法(修改订单状态为退款完成(2))
     * @param orderNum 修改状态订单的订单号
     * @return 返回修改后的数据和是否返回成功
     */
    @PostMapping("/refund")
    @ApiOperation(value = "退款的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNum",value = "同意退款订单的订单号",required = true,paramType = "query",dataType = "string")
    })
    public Map updateRefund(@RequestParam("orderNum") String orderNum) throws BusinessException {
        Boolean refund = refundService.refund(orderNum);
        JsonResult jsonResult = new JsonResult();
        if (refund) {
            jsonResult.setMsg("退款成功");
            jsonResult.setCode("1");
        }else {
            jsonResult.setMsg("退款失败");
            jsonResult.setCode("0");
        }
        return jsonResult.getDataMap();
    }

}
